package SVN::TeamTools::Plugins::Branching;

use warnings;
use strict;
use Carp;
use Error qw(:try);

use SVN::TeamTools::Store::Config;

use XML::Simple;
use HTML::Template;
use CGI;
use URI::Escape;
use Proc::Fork;
use Data::Dumper;

my $conf;
my $logger;
BEGIN { $conf = SVN::TeamTools::Store::Config->new(); $logger = $conf->{logger}; }

sub hasAction {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	return ("|dsp.branch.create|dsp.branch.merge|dsp.branch.diff|get.branches.schedule|add.branches.schedule|del.branches.schedule|" =~ /\|\Q$action\E\|/);
}

sub getTemplate {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	my $cgi		= $args{cgi};

	my $template;
	if ($action eq "dsp.branch.create") {
		$template = HTML::Template->new( filename => 'SVN/TeamTools/Plugins/tmpl/branching-create.tmpl', path => @INC );
	} elsif ($action eq "dsp.branch.merge") {
		$template = HTML::Template->new( filename => 'SVN/TeamTools/Plugins/tmpl/branching-merge.tmpl', path => @INC );
	}

	return $template;
}

sub getData {
	shift;
	my %args	= @_;
	my $action	= $args{action};
	my $param	= $args{param};

	if ($action eq 'get.branches.schedule') {
		my $xml = new XML::Simple;
		my $result;
		try {
			$result = $xml->XMLin("/etc/teamtools/branch-schedule.xml", ForceArray => [qw(times job path)]);
		} otherwise {
			$result=[];
		};

		return $result;
	} elsif ($action eq 'add.branches.schedule') {
		my %result = (
			status => 'OK',
			msg => ''
		);
		if (exists $param->{schedule} and $param->{schedule}->{operation} eq 1) {
			my $xml = new XML::Simple;
			my $data;
			try {
				$data = $xml->XMLin("/etc/teamtools/branch-schedule.xml", ForceArray => [qw(times job path)]);
			} otherwise {
				$data = {job=>[]};
			};

			delete $param->{action};
			push (@{$data->{job}}, $param);

			my $out	= $xml->XMLout($data, SuppressEmpty=>1, NoAttr=>1, RootName=>'MergeJob');
			open (my $conffile, ">", "/etc/teamtools/branch-schedule.xml") or die "Can't open configuration file (/etc/teamtools/branch-schedule.xml)for update";
			print $conffile $out;
			close $conffile;
			$result{msg} = 'Operation stored';
		} else {
			run_fork {
				child {
					SVN::TeamTools::Store::Repo->mergeBranches (
						target=>$param->{target},
						trunk=>$param->{trunk},
						message=>$param->{desc},
						addresses=>$param->{mailto},
						branches=>$param->{merge}->{path},
					);
				}
			};
			$result{msg} = 'Operation is being executed. An email notification will be send after completion';
		}
		return \%result;
	} elsif ($action eq 'del.branches.schedule') {
		my %result = (
			status => 'OK',
			msg => 'Schedule removed',
		);
		my $xml = new XML::Simple;
		my $data;
		try {
			$data = $xml->XMLin("/etc/teamtools/branch-schedule.xml", ForceArray => [qw(times job path)]);
			for (my $i=0; $i<scalar(@{$data->{job}}); $i++) {
				if ( $data->{job}->[$i]->{m_id} eq $param->{id}) {
					delete $data->{job}->[$i];
					last;
				}
			}
			my $out	= $xml->XMLout($data, SuppressEmpty=>1, NoAttr=>1, RootName=>'MergeJob');
			open (my $conffile, ">", "/etc/teamtools/branch-schedule.xml") or die "Can't open configuration file (/etc/teamtools/branch-schedule.xml)for update";
			print $conffile $out;
			close $conffile;

			return \%result;
		} otherwise {
			return \%result;
		};

	}
}
1;
